<template>
    <div class="login">
<van-form @submit="onSubmit">
  <van-cell-group inset>
    <van-field
      v-model="username"
      name="username"
      label="用户名"
      placeholder="用户名"
      :rules="[{ required: true, message: '请填写用户名' }]"
    />
    <van-field
      v-model="password"
      type="password"
      name="password"
      label="密码"
      placeholder="密码"
      :rules="[{ required: true, message: '请填写密码' }]"
    />
  </van-cell-group>
  <div style="margin: 16px;">
    <van-button round block type="primary" native-type="submit">
      登录
    </van-button>

  </div>
</van-form>
    <van-button class="btn" to="/register" round block type="primary" native-type="submit">
      注册
    </van-button>
    </div>
</template>

<script>
import { ref } from 'vue';
import{ Toast } from 'vant'
import {fetchlogin } from '@/API/goods.js'
import { useRouter } from 'vue-router';
import { useStore  } from 'vuex';
export default {
  setup() {
    const username = ref('');
    const password = ref('');
    const router = useRouter();
    const store = useStore();
    const onSubmit =async (values) => {
      let result = await fetchlogin(values)
      if(result.status ==1){
       console.log(router);
          Toast(result.message) 
      }else if(result.status == 0){
          console.log(result);
          store.commit('chanttoken',result.token)
          store.commit('setuser',result.userInfo)

          var e=store.getters.getsaverouter
        router.replace(e)  
      }
    };

    return {
      username,
      password,
      onSubmit,
    };
  },
};
</script>

<style lang="scss" scoped>
.login{
    margin-top: 0.5rem;
}
.btn{
    margin-top: 2rem;
    width: 14rem;
    margin: 0 auto;
}
</style>